import { history } from 'umi';
import {
  fetchAuthAccess,
  fetchAccessTree,
  fetchGetAccessTree,
  fakeAuthAccess,
  fetchListRole,
  fakeAuthRole,
  fakeRoleAccess,
  fetchListAdmin,
  fetchRoleTree,
  fakeAuthAdmin,
} from '@/services/SettingServices';
import { notification } from 'antd';
const setting = {
  namespace: 'setting',

  state: {
    list: {},
    allMenu: [],
    adminList: {},
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *fetchAuthList({ payload, callback }, { call, put }) {
      const response = yield call(fetchAuthAccess, payload);
      if (!response) return;
      // const { authAccessForUseResponseList } = response.content;
      // yield put({
      //   type: 'save',
      //   payload: {
      //     menuList: authAccessForUseResponseList,
      //   },
      // });
      callback && callback();
    },
    *fetchGetList({ payload, callback }, { call, put }) {
      const response = yield call(fetchAccessTree, payload);
      if (!response) return;
      const { authAccessList = [] } = response.content;
      yield put({
        type: 'save',
        payload: {
          list: {
            list: authAccessList,
            total: authAccessList.length,
          },
        },
      });
      callback && callback();
    },
    //获取角色资源关系
    *fetchGetAccessTree({ payload, callback }, { call, put }) {
      const response = yield call(fetchGetAccessTree, payload);
      if (!response) return;
      console.log();
      const { allAccessList = [] } = response.content;
      yield put({
        type: 'save',
        payload: {
          allMenu: allAccessList,
        },
      });
      callback && callback(response.content);
    },
    *fakeAuthAccess({ payload, callback }, { call, put }) {
      const response = yield call(fakeAuthAccess, payload);
      const { id } = payload;
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: `${id ? '修改' : '新增 '}成功`,
      });
      callback && callback();
    },
    //获取所有角色
    *fetchListRole({ payload, callback }, { call, put }) {
      const response = yield call(fetchListRole, payload);
      if (!response) return;
      console.log();
      const { authRoleList = [], count } = response.content;
      yield put({
        type: 'save',
        payload: {
          list: {
            list: authRoleList,
            total: count,
          },
        },
      });
    },
    //新增或修改角色
    *fakeAuthRole({ payload, callback }, { call, put }) {
      const response = yield call(fakeAuthRole, payload);
      const { id } = payload;
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: `${id ? '新增' : '修改'}成功`,
      });
      callback && callback();
    },
    //修改角色所关联资源
    *fakeRoleAccess({ payload, callback }, { call, put }) {
      const response = yield call(fakeRoleAccess, payload);
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: `修改成功`,
      });
      callback && callback();
    },

    //修改角色所关联资源
    *fetchListAdmin({ payload, callback }, { call, put }) {
      const response = yield call(fetchListAdmin, payload);
      if (!response) return;
      const { authAdminList, count } = response.content;
      yield put({
        type: 'save',
        payload: {
          adminList: {
            list: authAdminList,
            total: count,
          },
        },
      });
    },
    *fetchRoleTree({ payload, callback }, { call, put }) {
      const response = yield call(fetchRoleTree, payload);
      if (!response) return;
      callback && callback(response.content);
    },
    //保存或修改管理员
    *fakeAuthAdmin({ payload, callback }, { call, put }) {
      const response = yield call(fakeAuthAdmin, payload);
      const { id } = payload;
      if (!response) return;
      notification.success({
        message: '温馨提示',
        description: `${id ? '新增' : '修改'}成功`,
      });
      callback && callback();
    },
  },
};
export default setting;
